<?php
/**
 * 城市选择
 *
 * @copyright  Copyright (c) 2015-2025 NcPHP Inc. (http://www.ncphp.com)
 * @license    http://www.ncphp.com/
 * @link       http://www.ncphp.com/
 * @author     NcPHP Team
 * @since      File available since Release v1.1
 */
defined('InNcPHP') or exit('Access Invalid!');

class locationControl extends BaseHomeControl{

    public function __construct(){
        parent::__construct();
    }

    /**
     * 城市选择
     */
    public function indexOp(){
        /** @var areaModel $area */
        $area = Model('area');
        $ticket_model = Model('ticket');
        $_char = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z');
        $_city = array();
        foreach ($_char as $char) {$_city[$char] = $area->getAreaList(array('ar_first_py'=>$char,'type'=>2));
        }
        $data = $ticket_model->gethotcity();
        $arr  = '';
        foreach ($data as $k=>$v){
           $arr.= $v['tk_city_id'].',';
        }
        $where['area_id'] = array('in',$arr);
        $hot_city = $area->getAreaList($where,'area_id,name');
        Tpl::output('hot_city',$hot_city);
        Tpl::output('char', $_char);
        Tpl::output('city', $_city);
        Tpl::showpage('location.index');
    }

    /**
     * 城市搜索
     */
    public function searchOp()
    {
        $result = 0;
        $keyword = trim($_GET['keyword']);
        $condition = array();
        $condition['name'] = array('like', '%'.$keyword.'%');
        $condition['type'] = 2;
        /** @var areaModel $model */
        $model = Model('area');
        $city_list = $model->getAreaList($condition);
        if(count($city_list)){
            $result = 1;
        }
        die(json_encode(array('result'=>$result, 'data'=>$city_list)));
    }

    /**
     * 改变城市
     */
    public function changeOp()
    {
        /** @var areaModel $area */
        $area = Model('area');

        $city_id = intval($_POST['city']);
        $city = $area->getAreaInfo(array('area_id'=>$city_id));
        if ($city) {
            $_SESSION['pos']['city_id'] = $city['area_id'];
            $_SESSION['pos']['city_name'] = $city['name'];
            exit(json_encode(array('res'=>1)));
        }
        exit(json_encode(array('res'=>0)));
    }

    /**
     * 首次定位借助JS辅助定位
     */
    public function ajaxOp()
    {
        /** @var areaModel $area */
        $area = Model('area');

        $location = array(
            'lat' => $_GET['lat'],
            'lng' => $_GET['lng']
        );
        $url = 'http://restapi.amap.com/v3/geocode/regeo?key=5c7baac99d10897f7b2afa1aec0622e3&location='.$location['lng'].','.$location['lat'];
        $geocode = json_decode($this->httpGet($url));
        $cityName = $geocode->regeocode->addressComponent->city;
        $cityName = mb_substr(rtrim($cityName, '市'),0,3,'UTF-8');

        $condition = array();
        $condition['name'] = array('like', $cityName . '%');
        $condition['type'] = 2;
        $cityInfo = $area->getAreaInfo($condition);
        $reload = false;
        if ($cityInfo && $cityInfo['area_id'] != $_SESSION['pos']['city_id']) {
            $_SESSION['pos'] = array('city_id'=>$cityInfo['area_id'], 'city_name'=>$cityInfo['name']);
            $reload = true;
        }
        $_SESSION['pos']['js'] = true;
        $_SESSION['location']  = array('lat'=>$location['lng'],'lng'=>$location['lat']);
        exit(json_encode(array('reloads'=>$reload,'city'=>$cityName)));
    }


}
